package com.dmx.platform.action;

import java.io.File;
import java.util.Date;

import javax.servlet.ServletContext;

import org.apache.struts2.util.ServletContextAware;

import com.dmx.fw.constant.ViewConstant;
import com.dmx.fw.web.struts2.action.CommonQueryAction;
import com.dmx.platform.Constants;
import com.dmx.platform.entity.TBaseinfo;
import com.dmx.platform.entity.TImage;
import com.dmx.platform.service.InformationService;
import com.dmx.platform.utils.FileProcess;
import com.opensymphony.xwork2.ActionContext;

/**
 *@author: Beni Yang
 *@date:2012-8-18
 *@function:
 */
@SuppressWarnings("serial")
public class BaseInfoAction extends CommonQueryAction implements ServletContextAware{

	private static final String BATCH_DELETE = "batch_delete";
	private static final String VIEWMYINFO = "viewMyInfo";
	private static final String SUBMITMYINFO = "submitMyInfo";
	private static final String SUBMITIMAGE = "submitImage";
	private static final String DELETEIMAGE = "deleteImage";
	private String imageStorePath = "/upload";
	private TBaseinfo info;
	private InformationService infoService;

	private String[] ids;
	private String id;
	private String q_type;
	private String q_name;
	private String q_city;
	private String q_region;
	private String q_level;
	private String q_bstatus;
	
	private File uploadFile;
	private ServletContext context;


	// actions begin

	public String execute() throws Exception {
		super.busiId = "searchBaseInfo";
		if (ViewConstant.QUERY.equals(super.act)) {
			doQuery();
		} else if (ViewConstant.REQUERY.equals(super.act)) {
			doReQuery();
		} else if (ViewConstant.DELETE.equals(super.act)) {
			this.doDelete();
		} else if (BATCH_DELETE.equals(super.act)) {
			doBatchDelete();
		} else if (ViewConstant.ADDINPUT.equals(super.act)) {
			this.pageState = ViewConstant.ADDSTATUS;
			return doAddInput();
		} else if (ViewConstant.ADDSUBMIT.equals(super.act)) {
			return doAddSubmit();
		} else if (ViewConstant.EDITINPUT.equals(super.act)) {
			this.pageState = ViewConstant.EDITSTATUS;
			return doEditInput();
		} else if (ViewConstant.EDITSUBMIT.equals(super.act)) {
			return doEditSubmit();
		} else if (ViewConstant.VIEW.equals(super.act)) {
			this.pageState = ViewConstant.VIEWSTATUS;
			return doEditInput();
		} else if (VIEWMYINFO.equals(super.act)) {
			return doVeiwMyInfo();
		} else if (SUBMITMYINFO.equals(super.act)) {
			return doSubmitMyInfo();
		} else if (SUBMITIMAGE.equals(super.act)) {
			return doSubmitImage();
		} else if (DELETEIMAGE.equals(super.act)) {
			return doDeleteImage();
		}

		else {
			super.execute();
		}
		return SUCCESS;
	}

	public String doSubmitImage() {
		if(info!=null){
//			String path=FileProcess.createPath(this.getImageStorePath(),info.getBaseId());
			String path=FileProcess.createPath(this.getContext().getRealPath(this.getImageStorePath()),info.getBaseId());
			String filename=String.valueOf(System.currentTimeMillis())+".jpg";
			String imageName=null;
			String imageUrl=this.getImageStorePath()+File.separatorChar+info.getBaseId()+File.separatorChar+filename;
			if(path!=null){
				imageName=FileProcess.uploadFile(path, filename, uploadFile);
				if(imageName!=null){
					log.info("Upload image sucessful : "+imageUrl);
//				save image information to db
					TImage image=new TImage();
					image.setOwnerId(info.getBaseId());
					image.setUrl(imageUrl);
					this.infoService.saveImage(image);
					
					this.setSuccessinfo("上传图片成功！");
				}else{
					this.setErrorInfo("上传图片失败！");
				}
			}
		}

		return doVeiwMyInfo();
	}



	public String doDeleteImage() {
		
		TImage image=this.infoService.getImageById(id);
		String pathfile=this.getContext().getRealPath(image.getUrl());
		log.warn(pathfile+" will be deleted...");
		FileProcess.deleteFile(pathfile);
		if(this.infoService.deleteImage(id)){
			this.setSuccessinfo("删除图片成功！");
		}else{
			this.setErrorInfo("删除图片失败，请刷新后重试！");
		}
		return doVeiwMyInfo();
	}

	public String doAddInput() {

		return ViewConstant.ADDINPUT;
	}

	public String doAddSubmit() throws Exception {

		// UserBaseInfo tempUserBaseInfo =
		// userManageService.findUserBaseInfoById(userBaseInfo.getUid());
		// if(tempUserBaseInfo!=null){
		// super.setErrorInfo(super.getText("cep.userBaseInfo.warning.userid.duplicate"));
		// return ViewConstant.ADDINPUT;
		// }
		try {
			info.setRecordTime(new Date());
			infoService.saveOrUpdateBaseInfo(info);
			setSuccessinfo(getText("info.addSuccess"));
			super.doReQuery();
		} catch (Exception e) {
			log.error(e);
			this.setErrorInfo(this.getText("err.system.UnknownException"));
			return doEditInput();
		}
		return SUCCESS;
	}

	public String doEditInput() {
		if (this.id != null) {
			info = infoService.findBaseInfoById(id);
		}
		return ViewConstant.EDITINPUT;
	}

	public String doVeiwMyInfo() {
		info = infoService.findBaseInfoById(this.getBusiDataId());

		return VIEWMYINFO;
	}

	public String doSubmitMyInfo() {
		try {
			this.info.getIcon();
			info.setRecordTime(new Date(System.currentTimeMillis()));
			infoService.saveOrUpdateBaseInfo(info);
			setSuccessinfo(getText("info.addSuccess"));
			return doVeiwMyInfo();
		} catch (Exception e) {
			log.error(e);
			this.setErrorInfo(this.getText("err.system.UnknownException"));
		}
		return doVeiwMyInfo();
	}

	public String doEditSubmit() throws Exception {

		try {
			info.setRecordTime(new Date(System.currentTimeMillis()));
			infoService.saveOrUpdateBaseInfo(info);
			setSuccessinfo(getText("info.addSuccess"));
			super.doReQuery();
		} catch (Exception e) {
			log.error(e);
			this.setErrorInfo(this.getText("err.system.UnknownException"));
			return doEditInput();
		}
		return SUCCESS;
	}

	public String doBatchDelete() throws Exception {
		// userManageService.batchDeleteUserBaseInfoByIds(ids);
		// userManageService.batchDeleteUserInfoByIds(ids);
		return super.doQuery();
	}

	public String doDelete() throws Exception {
		if (this.id != null) {
			infoService.deleteBaseInfoById(id);
		}
		setSuccessinfo(getText("info.deleteSuccess"));
		return super.doQuery();
	}

	public TBaseinfo getInfo() {
		return info;
	}

	public void setInfo(TBaseinfo info) {
		this.info = info;
	}

	public InformationService getInfoService() {
		return infoService;
	}

	public void setInfoService(InformationService infoService) {
		this.infoService = infoService;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getQ_type() {
		return q_type;
	}

	public void setQ_type(String qType) {
		q_type = qType;
	}

	public String getQ_name() {
		return q_name;
	}

	public void setQ_name(String qName) {
		q_name = qName;
	}

	public String getQ_city() {
		return q_city;
	}

	public void setQ_city(String qCity) {
		q_city = qCity;
	}

	public String getQ_region() {
		return q_region;
	}

	public void setQ_region(String qRegion) {
		q_region = qRegion;
	}

	public String getQ_level() {
		return q_level;
	}

	public void setQ_level(String qLevel) {
		q_level = qLevel;
	}

	public String getQ_bstatus() {
		return q_bstatus;
	}

	public void setQ_bstatus(String qBstatus) {
		q_bstatus = qBstatus;
	}


	public String getImageStorePath() {
		return imageStorePath;
	}

	public void setImageStorePath(String imageStorePath) {
		this.imageStorePath = imageStorePath;
	}

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public ServletContext getContext() {
		return context;
	}

	public void setContext(ServletContext context) {
		this.context = context;
	}

	@Override
	public void setServletContext(ServletContext arg0) {
		// TODO Auto-generated method stub
		this.context=arg0;
	}



}